##
##The following code replicates The ZIOPC Marginal Effects Tables appearing in the supplemental appendix to our main paper
##

rm(list=ls())  #clear  memory                                             
library(car)
library(Hmisc)                                                     
library(scatterplot3d)
library(mvtnorm)
library(foreign)
library(pscl)
library(graphics)
library(MASS)
library(lattice)
library(tseries)
library(Matrix)
library(nlme)
library(grDevices)
library(pastecs)
library(moments)
library(splines)
library(gdata, gtools)
library(gmodels)
library(msm)
library(corpcor)

#choose a true-zero level (ranging from 0 to 100, or All)
a<-100


######################Read in Main Results####################
data<-read.dta(paste("Marginal.Effects.", a,".True.ZIOPCDGP.dta",sep=""))

one<-"\\begin{table}[tbp]"
two<-paste("\\","caption{Marginal Effects For ZIOPC DGDP with ", a, "\\% True Zeroes}",sep="")
three<-paste("\\","label{tab:ZIOPC.results.",a,"}",sep="")
four<-"\\begin{center}"
five<-"{\\footnotesize"
six<-"\\begin{tabular}{cccccc|ccccc}"
seven<-"\\hline\\hline"
eight<-"	&	&\\multicolumn{4}{c}{\\underline{$Pr(Y=0)$ Marginal Effect}}&	&\\multicolumn{4}{c}{\\underline{$Pr(Y=1)$ Marginal Effect}}	\\\\"
nine<-"	&	&True	&OP	&ZiOP	&ZiOPC&	&True	&OP	&ZiOP	&ZiOPC	\\\\"
ten<-paste("$x_{1}$&$Mean$&", sprintf("%.3f",round(data$True_ME_X1_Y0,3)),"&",sprintf("%.3f",round(data$OP_ME_X1_Y0,3)),"&",sprintf("%.3f",round(data$ZIOP_ME_X1_Y0,3)),"&",sprintf("%.3f",round(data$ZIOPC_ME_X1_Y0,3)),"&","$x_{1}$&", sprintf("%.3f",round(data$True_ME_X1_Y1,3)),"&",sprintf("%.3f",round(data$OP_ME_X1_Y1,3)),"&",sprintf("%.3f",round(data$ZIOP_ME_X1_Y1,3)),"&",sprintf("%.3f",round(data$ZIOPC_ME_X1_Y1,3)),"\\\\",sep="")
eleven<-paste("&$RMSE$&","&(",sprintf("%.3f",round(data$OP_RMSE_X1_Y0,3)),")&(",sprintf("%.3f",round(data$ZIOP_RMSE_X1_Y0,3)),")&(",sprintf("%.3f",round(data$ZIOPC_RMSE_X1_Y0,3)),")&","&","&(",sprintf("%.3f",round(data$OP_RMSE_X1_Y1,3)),")&(",sprintf("%.3f",round(data$ZIOP_RMSE_X1_Y1,3)),")&(",sprintf("%.3f",round(data$ZIOPC_RMSE_X1_Y1,3)),")\\\\",sep="")
twelve<-paste("&$CP$&","&",sprintf("%.3f",round(data$OP_CP_X1_Y0,3)),"&",sprintf("%.3f",round(data$ZIOP_CP_X1_Y0,3)),"&",sprintf("%.3f",round(data$ZIOPC_CP_X1_Y0,3)),"&","&","&",sprintf("%.3f",round(data$OP_CP_X1_Y1,3)),"&",sprintf("%.3f",round(data$ZIOP_CP_X1_Y1,3)),"&",sprintf("%.3f",round(data$ZIOPC_CP_X1_Y1,3)),"\\\\",sep="")
thirteen<-"	&	&&&	&	&	&&	&	&	\\\\"
fourteen<-"	&	&True	&OP	&ZiOP	&ZiOPC	&&True	&OP	&ZiOP	&ZiOPC	\\\\"
fifteen<-paste("$x_{2}$&$Mean$&", sprintf("%.3f",round(data$True_ME_X2_Y0,3)),"&",sprintf("%.3f",round(data$OP_ME_X2_Y0,3)),"&",sprintf("%.3f",round(data$ZIOP_ME_X2_Y0,3)),"&",sprintf("%.3f",round(data$ZIOPC_ME_X2_Y0,3)),"&","$x_{2}$&",sprintf("%.3f",round(data$True_ME_X2_Y1,3)),"&",sprintf("%.3f",round(data$OP_ME_X2_Y1,3)),"&",sprintf("%.3f",round(data$ZIOP_ME_X2_Y1,3)),"&",sprintf("%.3f",round(data$ZIOPC_ME_X2_Y1,3)),"\\\\",sep="")
sixteen<-paste("&$RMSE$&","&(",sprintf("%.3f",round(data$OP_RMSE_X2_Y0,3)),")&(",sprintf("%.3f",round(data$ZIOP_RMSE_X2_Y0,3)),")&(",sprintf("%.3f",round(data$ZIOPC_RMSE_X2_Y0,3)),")&","&","&(",sprintf("%.3f",round(data$OP_RMSE_X2_Y1,3)),")&(",sprintf("%.3f",round(data$ZIOP_RMSE_X2_Y1,3)),")&(",sprintf("%.3f",round(data$ZIOPC_RMSE_X2_Y1,3)),")\\\\",sep="")
seventeen<-paste("&$CP$&","&",sprintf("%.3f",round(data$OP_CP_X2_Y0,3)),"&",sprintf("%.3f",round(data$ZIOP_CP_X2_Y0,3)),"&",sprintf("%.3f",round(data$ZIOPC_CP_X2_Y0,3)),"&","&","&",sprintf("%.3f",round(data$OP_CP_X2_Y1,3)),"&",sprintf("%.3f",round(data$ZIOP_CP_X2_Y1,3)),"&",sprintf("%.3f",round(data$ZIOPC_CP_X2_Y1,3)),"\\\\",sep="")
seventeen.2<-"\\hline\\hline"
eighteen<-"	&	&\\multicolumn{4}{c}{\\underline{$Pr(Y=2)$ Marginal Effect}}&	&\\multicolumn{4}{c}{\\underline{Estimates}}	\\\\"
nineteen<-"	&	&True	&OP	&ZiOP	&ZiOPC	&&True	&OP	&ZiOP	&ZiOPC	\\\\"
twenty<-paste("$x_{1}$&$Mean$&", sprintf("%.3f",round(data$True_ME_X1_Y2,3)),"&",sprintf("%.3f",round(data$OP_ME_X1_Y2,3)),"&",sprintf("%.3f",round(data$ZIOP_ME_X1_Y2,3)),"&",sprintf("%.3f",round(data$ZIOPC_ME_X1_Y2,3)),"&","$\\tau_{2}$&",sprintf("%.3f",round(data$True_T2,3)),"&",sprintf("%.3f",round(data$OP_T2,3)),"&",sprintf("%.3f",round(data$ZIOP_T2,3)),"&",sprintf("%.3f",round(data$ZIOPC_T2,3)),"\\\\",sep="")
twentyone<-paste("&$RMSE$&","&(",sprintf("%.3f",round(data$OP_RMSE_X1_Y2,3)),")&(",sprintf("%.3f",round(data$ZIOP_RMSE_X1_Y2,3)),")&(",sprintf("%.3f",round(data$ZIOPC_RMSE_X1_Y2,3)),")&","&","&(",sprintf("%.3f",round(data$OP_RMSE_T2,3)),")&(",sprintf("%.3f",round(data$ZIOP_RMSE_T2,3)),")&(",sprintf("%.3f",round(data$ZIOPC_RMSE_T2,3)),")\\\\",sep="")
twentytwo<-paste("&$CP$&","&",sprintf("%.3f",round(data$OP_CP_X1_Y2,3)),"&",sprintf("%.3f",round(data$ZIOP_CP_X1_Y2,3)),"&",sprintf("%.3f",round(data$ZIOPC_CP_X1_Y2,3)),"&","&","&",sprintf("%.3f",round(data$OP_CP_T2,3)),"&",sprintf("%.3f",round(data$ZIOP_CP_T2,3)),"&",sprintf("%.3f",round(data$ZIOPC_CP_T2,3)),"\\\\",sep="")
twentythree<-"	&	&&&	&	&	&	&&	&	\\\\"
twentyfour<-"	&	&True	&OP	&ZiOP	&ZiOPC	&&True	&OP	&ZiOP	&ZiOPC	\\\\"
twentyfive<-paste("$x_{2}$&$Mean$&", sprintf("%.3f",round(data$True_ME_X2_Y2,3)),"&",sprintf("%.3f",round(data$OP_ME_X2_Y2,3)),"&",sprintf("%.3f",round(data$ZIOP_ME_X2_Y2,3)),"&",sprintf("%.3f",round(data$ZIOPC_ME_X2_Y2,3)),"&","$\\rho$&", sprintf("%.3f",round(data$True_R,3)),"&",".","&",".","&",sprintf("%.3f",round(data$ZIOPC_R,3)),"\\\\",sep="")
twentysix<-paste("&$RMSE$&","&(",sprintf("%.3f",round(data$OP_RMSE_X2_Y2,3)),")&(",sprintf("%.3f",round(data$ZIOP_RMSE_X2_Y2,3)),")&(",sprintf("%.3f",round(data$ZIOPC_RMSE_X2_Y2,3)),")&","&","&",".","&",".","&(",sprintf("%.3f",round(data$ZIOPC_RMSE_R,3)),")\\\\",sep="")
twentyseven<-paste("&$CP$&","&",sprintf("%.3f",round(data$OP_CP_X2_Y2,3)),"&",sprintf("%.3f",round(data$ZIOP_CP_X2_Y2,3)),"&",sprintf("%.3f",round(data$ZIOPC_CP_X2_Y2,3)),"&","&","&",".","&",".","&",sprintf("%.3f",round(data$ZIOPC_CP_R,3)),"\\\\",sep="")
twentyeight<-"\\hline\\hline"
twentynine<-"\\end{tabular}}"
thirty<-"\\end{center}"
thirtyone<-"\\end{table}"

table<-rbind(one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,seventeen.2,eighteen,nineteen,twenty,twentyone,twentytwo,twentythree,twentyfour,twentyfive,twentysix,twentyseven,twentyeight,twentynine,thirty,thirtyone)

#save table
write.table(table,paste("Table.", a,".True.ZIOPCDGP.txt",sep=""), quote=FALSE,col.names = FALSE,row.names = FALSE)

#The End